
$(function(){
    function checkAcct(){
        var testStr = /^.{1,9}$/;
        var $this = $(this);
        var id = $this.get(0).id;
        var name = $(this).val();
        if(testStr.test(name)){
            $.ajax({
                url:"/doUser",
                type:"post",
                data:{method:"doFindUser",name:name},
                success : function (date) {
                    if(date == 0){
                        $this.next().next().html("");
                    }else if(id != 'editUser') {
                        $this.next().next().html("账号已被注册");
                    }else if($('#editAcct').text() == $('#editUser').val()) {
                        $this.next().next().html("");
                    }else{
                        $this.next().next().html("账号已被注册");
                    }
                }
            })
        }else{
            $(this).next().next().html("账号不能为空");
        }
    }

    function check(){
        var testStr = /^.{1,9}$/;
        var $this = $(this);
        var id = $this.get(0).id;
        if(id == 'add_Account_ok_btn'){
            $this = $(this).prev().prev().find("#addAcct");
        }else if(id == 'user_column_ok_btn'){
            $this = $(this).prev().prev().find("#editUser");
        }
        var name = $(this).val();
        if(testStr.test(name)){
            $.ajax({
                url:"/doUser",
                type:"post",
                dataType : "text/html",
                data:{method:"doFindUser",name:name},
                success : function (date){
                    if(date == 0){
                        $this.next().next().html("");
                    }else if(id != 'user_column_ok_btn') {
                        $this.next().next().html("账号已被注册");
                    }else if($('#editAcct').text() == $('#editUser').val()) {
                        $this.next().next().html("");
                    }else{
                        $this.next().next().html("账号已被注册");
                    }
                }
            })
        }else{
            $(this).next().next().html("账号不能为空");
        }
        var pwd = $this.parent().next().find("input").val();
        var pwdErr = $this.parent().next().find("span");
        if(testStr.test(pwd)){
            pwdErr.html("");
        }else{
            pwdErr.html("密码不能为空");
        }

        if(id == 'add_Account_ok_btn' && $this.next().next().text() == "" && pwdErr.text() == ""){
            $("#add_Account").fadeOut(100);
            var acct = $("#addAcct").val();
            var pwd = $("#addPwd").val();
            window.location.href="/doUser?method=addUser&username="+acct+"&pwd="+pwd
        }else if(id == 'user_column_ok_btn' && $this.next().next().text() == "" && pwdErr.text() == ""){
            $("#user_column").fadeOut(100);
            var acct = $('#editUser').val();
            var pwd = $('#editPwd').val();
            var oldAcct = $('#editAcct').html();
            window.location.href = "/doUser?method=editUser&editUserAcct="+acct+"&editUserPwd="+pwd+"&oldAcct="+oldAcct;
        }
    }
    function checkPWD(){
        var testStr = /^.{1,9}$/;
        if(testStr.test($(this).val())){
            $(this).next().next().html("");
        }else{
            $(this).next().next().html("密码不能为空");
        }
    }
    function checkChnlName(){
        var testStr = /^.{1,9}$/;
        var $this = $(this);
        var id = $(this).get(0).id;
        var chnlName = $(this).val();
        var $chnl = $('input[name="chnl"]:checked');
        var checkedName;
        $chnl.each(function (i){
            checkedName = $(this).parent().parent().next().find('label[name=chnlName]').text();
            return false;
        })
        if(testStr.test(chnlName)){
            $.ajax({
                url : "/doChnl",
                type : "post",
                data : {method:"doFindChnl",findChnlName:chnlName},
                dataType: "text",
                success : function (data){
                    if(data == 0){
                        $this.next().next().html("");
                    }else if(id != 'editChnlName'){
                        $this.next().next().html("栏目名已存在");
                    }else if(checkedName == chnlName){
                        $this.next().next().html("");
                    }else{
                        $this.next().next().html("栏目名已存在");
                    }
                }
            })
        }else {
            $this.next().next().html("栏目名不能为空");
        }
    }
    function checkChnlNum(){
        var testStr = /^\d{1,9}$/;
        var $this = $(this);
        var $chnl = $('input[name="chnl"]:checked');
        var checkedID;
        $chnl.each(function (i){
            checkedID = $(this).next().text();
            return false;
        })
        if(testStr.test($(this).val())){
            $.ajax({
                url : "/doChnl",
                type : "post",
                data : {method:"doFindChnl",findChnlID:$this.val() },
                dataType: "text",
                success : function (data){

                    if(data == 0){
                        $this.next().next().html("");
                    }else if($this[0].id != 'editChnlNum'){
                        $this.next().next().html("代号已被使用");
                    }else if($this.val() == checkedID){
                        $this.next().next().html("");
                    }else{
                        $this.next().next().html("代号已被使用");
                    }
                }
            })
        }else{
            $(this).next().next().html("序号需要为数字");
        }
    }
    function checkChnl(){
        var testStr = /^.{1,9}$/;
        var $this = $(this);
        var id = $(this).get(0).id;
        var chnlName = $this.prev().prev().find('input');
        var chnlNameVal = chnlName.val();
        var $chnl = $('input[name="chnl"]:checked');
        var checkedName;
        $chnl.each(function (i){
            checkedName = $(this).parent().parent().next().find('label[name=chnlName]').text();
            return false;
        })
        if(testStr.test(chnlNameVal)){
            $.ajax({
                url : "/doChnl",
                type : "post",
                data : {method:"doFindChnl",findChnlName:chnlNameVal},
                dataType: "text",
                success : function (data){
                    if(data == 0){
                        chnlName.next().next().html("");
                    }else if(id != 'edit_column_ok_btn'){
                        chnlName.next().next().html("栏目名已存在");
                    }else if(checkedName == chnlNameVal){
                        chnlName.next().next().html("");
                    }else{
                        chnlName.next().next().html("栏目名已存在");
                    }
                }
            })
        }else {
            chnlName.next().next().html("栏目名不能为空");
        }
        var num = chnlName.parent().next().find("input");
        if(/^\d{1,9}$/.test(num.val())){
            num.next().next().html("")
        }else {
            num.next().next().html("需要需要为数字")
        }

        if (id == 'add_column_ok_btn' && num.next().next().text() == "" && chnlName.next().next().text() == ""){
            $("#add_column ").fadeOut(100);
            var addChnlName = $('#addChnlName').val();
            var addChnlNum = $('#addChnlNum').val();
            window.location.href = "/doChnl?method=doAddChnl&addChnlName="+addChnlName+"&addChnlNum="+addChnlNum;
        }else if(id == 'edit_column_ok_btn' && num.next().next().text() == "" && chnlName.next().next().text() == ""){
            if($chnl.is(":checked")){
                $("#edit_column ").fadeOut(100);
                var editChnlID = "";
                $chnl.each(function (i){
                    editChnlID = $(this).next().next().val();
                    return false;
                })
                var editName = $('#editChnlName').val();
                var editNum = $('#editChnlNum').val();
                window.location.href = "/doChnl?method=doEditChnl&editChnlName="+editName+"&editChnlNum="+editNum+"&editChnlID="+editChnlID;
            }
        }
    }
    // 适配
    // iframe
    $(".AccountManagement_c_iframe").css("height",$(document).height());
    // 导航高度
    $(".nav_side").css("height",$(document).height()-100);
    
    // iframe  链接
    $(".AccountManagement").click(function(){
        $(".AccountManagement_c_iframe iframe").attr("src","page/AccountManagement.jsp")
    })
    $(".ColumnManagement").click(function(){
        $(".AccountManagement_c_iframe iframe").attr("src","/doChnl?method=fistLoadChnlPage")
    })
    $(".NewsManagement").click(function(){
        $(".AccountManagement_c_iframe iframe").attr("src","/doNews?method=fistLoad")
    })


	// 用户界面   翻页
	$("#userPage ul li[class != 'on']").click(function(){
	    window.location.href = "/doUser?method=doUserPage&pageIndex=" + $(this).attr('name');
	});
    $("#userPage div").click(function (){
        window.location.href = "/doUser?method=doUserPage&pageIndex=" +  $(this).attr(`name`);
    })
    //栏目翻页
    $('#chnlPage ul li[class != "on"]').click(function (){
        window.location.href = "/doChnl?method=doUpdatePage&chnlPageIndex="+$(this).attr("name");
    })
    $('#chnlPage div').click(function (){
        window.location.href = "/doChnl?method=doUpdatePage&chnlPageIndex="+$(this).attr("name");
    })
    //新闻翻页
    $('#newsPage ul li[class != "on"]').click(function (){
        window.location.href = "/doNews?method=doUpdateNewsPage&newsPageIndex=" + $(this).attr('name');
    })
    $('#newsPage div').click(function (){
        window.location.href = "/doNews?method=doUpdateNewsPage&newsPageIndex=" + $(this).attr('name');
    })
	// nav
	$(".nav_side>div").click(function(){
		var index = $(this).index();
		$(this).addClass("active").siblings().removeClass("active");
		$(".main_fx>div").eq(index).show().siblings().hide();
        if( index == 0 ){
            $(".user_location span").text("账户管理");
        }else if( index == 1){
            $(".user_location span").text("栏目管理");
        }else if( index == 2){
            $(".user_location span").text("新闻管理");
        }
	})
    //退出
    $('#exit').click(function (){
        window.location.href = "/doUser?method=doExit";
    })



	// 弹窗   all
	// z账户管理 -
	// 添加账户
    $("#add_Account_btn").click(function(){
        $("#add_Account").fadeIn(100);
    })
    $('#addAcct').blur(checkAcct);
    $('#addPwd').blur(checkPWD);
    //修改账户
    $('#editUser').blur(checkAcct);
    $('#editPwd').blur(checkPWD);

    $(".add_Account_close").click(function(){
        $("#add_Account").fadeOut(100);
        $("#user_column").fadeOut(100);
        $("#delete_Account").fadeOut(100);
        $("#ac_Account").fadeOut(100);
        $("#add_column").fadeOut(100);
        $("#edit_column").fadeOut(100);
        $("#delete_column").fadeOut(100);
        $("#delete_news").fadeOut(100);
        $("#release_news").fadeOut(100);
        $('#edit_news').fadeOut(100);
    })
    // 编辑账户
    $("#edit_Account_btn").click(function(){
        var $input = $('input[name=user]:checked');
        if($input[0].checked){
            $("#user_column").fadeIn(100);
            var edit = "";
            $input.each(function (i){
                edit = $(this).next().html();
                return false;
            })
            $('#editAcct').html(edit);
            $('#editUser').val(edit);
        }
		//$("#user_column input[name='username']").val('aaaaa');
    })
    //删除账户
    $("#delete_Account_btn").click(function(){
        var $input = $('input[name=user]:checked');
        if($input.is(':checked')){
            $("#delete_Account").fadeIn(100);
            var delUser = "";
            $input.each(function (i){
                var html1 = $(this).next().html();
                delUser= i == 0 ?  delUser += html1 : delUser += ","+html1;
            })
            $('#deleteAcct').html(delUser);
        }
    })
    // 编辑账户授权
    $("#ac_Account_btn").click(function(){
        var $input = $('input[name=user]:checked');
        if($input.is(':checked')){
            var $parents;
            $input.each(function (i){
                $parents = $(this).parents("ul[class=list_b]");
                return false;
            })
            var $userChnl;
            $parents.each(function (i){
                $userChnl = $(this).find("#userChnl");
                return false;
            })
            var userChnl = $userChnl.text();
            var $span = $("span[name=allChnl]");
            var split = userChnl.split(",");
            for (var i = 0; i < split.length; i++) {
                for (var j = 0; j < $span.length; j++) {
                    if(split[i] == $span[j].innerHTML){
                        $span[j].previousSibling.previousSibling.checked = true;
                    }
                }
            }
            $("#ac_Account").fadeIn(100);
        }
    })


    // 栏目管理 -
    $("#add_column_btn").click(function(){
        $("#add_column").fadeIn(100);
    })
    // 编辑栏目
    $("#edit_column_btn").click(function(){
        var $chnl = $('input[name="chnl"]:checked');
        if($chnl.is(":checked")){
            $("#edit_column").fadeIn(100);
            var editChnlName = "";
            var editChnlNum = "";
            $chnl.each(function (i){
                editChnlName = $(this).parents("ul[class=list_b]").find('label[name=chnlName]').text();
                editChnlNum = $(this).next().text();
                return false;
            })
            $('#editChnlName').val(editChnlName);
            $('#editChnlNum').val(editChnlNum);
        }
    })
    // 删除栏目
    $("#delete_column_btn").click(function(){
        var $chnl = $('input[name="chnl"]:checked');
        if($chnl.is(":checked")){
            $("#delete_column").fadeIn(100);
            var delChnlName = "";
            $chnl.each(function (i){
                var text = $(this).parents("ul[class=list_b]").find('label[name=chnlName]').text();
                delChnlName = i == 0 ? delChnlName += text : delChnlName += ","+text;
            })
            $('#delChnl').text(delChnlName);
        }
    })

    // 确定 btn 
    $("#add_Account_ok_btn").click(check
        // $("#add_Account").fadeOut(100);
        // var acct = $("#addAcct").val();
        // var pwd = $("#addPwd").val();
        // window.location.href="/doUser?method=addUser&username="+acct+"&pwd="+pwd;
    )
    $("#user_column_ok_btn").click(check
        // $("#user_column").fadeOut(100);
        // var acct = $('#editUser').val();
        // var pwd = $('#editPwd').val();
        // var oldAcct = $('#editAcct').html();
        // window.location.href = "/doUser?method=editUser&editUserAcct="+acct+"&editUserPwd="+pwd+"&oldAcct="+oldAcct;}
        )
    $("#delete_Account_ok_btn").click(function(){
        $("#delete_Account").fadeOut(100);
        var html = $('#deleteAcct').html();
        window.location.href = "/doUser?method=delUser&delUserAcct="+html;
    })
    $("#ac_Account_ok_btn").click(function(){
        $("#ac_Account").fadeOut(100);
        var $chnl = $('input[name=chnlName]');
        if($chnl.is(':checked')){
            var $input = $('input[name=user]:checked');
            var acct;
            $input.each(function (i){
                acct = $(this).next().text();
                return false;
            })
            var chnl = "";
            $chnl.each(function (i){
                if($(this).is(':checked')){
                    chnl += $(this).next().text() + ",";
                }
            })
            window.location.href = "/doUser?method=doAccreditUser&accUser="+acct+"&chnlName="+chnl;
        }else {
            alert("请至少选择一个栏目");
        }
    })

    $("#addChnlName").blur(checkChnlName);
    $('#addChnlNum').blur(checkChnlNum);
    $('#editChnlName').blur(checkChnlName);
    $('#editChnlNum').blur(checkChnlNum);

    $("#add_column_ok_btn").click(checkChnl
        // $("#add_column ").fadeOut(100);
        // var addChnlName = $('#addChnlName').val();
        // var addChnlNum = $('#addChnlNum').val();
        // window.location.href = "/doChnl?method=doAddChnl&addChnlName="+addChnlName+"&addChnlNum="+addChnlNum;
    )
    $('#editChnlNum').blur(checkChnlNum);
    $("#edit_column_ok_btn").click(checkChnl
        // var $chnl = $('input[name="chnl"]:checked');
        // if($chnl.is(":checked")){
        //     $("#edit_column ").fadeOut(100);
        //     var editChnlID = "";
        //     $chnl.each(function (i){
        //         editChnlID = $(this).next().next().val();
        //         return false;
        //     })
        //     var editName = $('#editChnlName').val();
        //     var editNum = $('#editChnlNum').val();
        //     window.location.href = "/doChnl?method=doEditChnl&editChnlName="+editName+"&editChnlNum="+editNum+"&editChnlID="+editChnlID;
        // }
    )

    $("#delete_column_ok_btn").click(function(){
        $("#delete_column").fadeOut(100);
        window.location.href = "/doChnl?method=doDelChnl&delChnlName="+$('#delChnl').text();
    })
    //搜索用户
    $("#searchUser").click(function (){
        var val = $('#selAcct').val();
        window.location.href = "/doUser?method=searchUser&selAcct="+val;
    })
    //搜索项目
    $('#searchChnl').click(function (){
        var selChnl = $('#selChnl').val();
        window.location.href = "/doChnl?method=doSearchChnl&selChnl="+selChnl;
    })
    //新闻检查
    $('#addNewsCenter').blur(function (){
        var centerTest = /^.{3,24}$/
        var centerVal = $(this).val();
        if(centerTest.test(centerVal)){
            $("#newsErrMSG").html("");
        }else{
            $("#newsErrMSG").html("标题太短或太长");
        }
    })
    $('#editNewsCenter').blur(function (){
        var centerTest = /^.{3,24}$/
        var centerVal = $(this).val();
        if(centerTest.test(centerVal)){
            $("#editNewsErrMSG").html("");
        }else{
            $("#editNewsErrMSG").html("标题太短或太长");
        }
    })

    $('#addNewsButton').click(function (){
        var $err = $(this).next();
        var centerTest = /^.{3,24}$/
        var textTs=/^.{10,300}$/;
        if(!centerTest.test($("#addNewsCenter").val())){
            $("#newsErrMSG").html("标题太短或太长");
        }else if($('#addNewsSelectID').val() == "==请选择=="){
            $err.html("请选择栏目")
        }else if(!textTs.test($('#addNewsText').val())){
            $err.html("文本内容不得少于10个字符或多于300个字符");
        }else{
            $.ajax({
                url : "/doNews",
                type : "post",
                data : {method:"checkTitle",newsTitle:$("#addNewsCenter").val(),newsChnlName:$('#addNewsSelectID').val()},
                dataType : "text",
                async : false,
                success : function (data){
                    if(data == 0){
                        $err.html("此栏目下本标题已被使用");
                    }else{
                        $err.html("");
                    }
                }
            })
        }

        if($err.text() == "" && $("#newsErrMSG").text() == ""){
            window.location.href="/doNews?method=addNews&addNewsTitle="+$("#addNewsCenter").val()+"&addNewsKeyword="+$('#addNewsKeyword').val()+
            "&addNewsChnlName="+$('#addNewsSelectID').val()+"&addNewsTop="+$('#addNewsSelectTop').val()+"&addNewsText="+$('#addNewsText').val();
        }

    })

    //编辑新闻
    $('#editNewsButton').click(function (){
        var $checked = $('input[name=news]:checked');
        var oldTitle = $checked.eq(0).nextAll(":last").text();
        var oldChnl = $checked.eq(0).parents("ul[class=list_b]").find('label[name="chnlName"]').text();
        var newsID = $checked.eq(0).next().attr("id");
        var $err = $(this).next();
        var centerTest = /^.{3,24}$/
        var textTs=/^.{10,300}$/;
        if($checked.is(':checked')){
            var newsTitle = $("#editNewsCenter").val();
            var newsChnl = $('#editNewsSelectID').val();
            if(!centerTest.test(newsTitle)){
                $("#editNewsErrMSG").html("标题太短或太长");
            }else if(newsTitle == oldTitle && newsChnl == oldChnl){
                $("#editNewsErrMSG").html("");
            }else{
                $.ajax({
                    url : "/doNews",
                    type : "post",
                    data : {method:"checkTitle",newsTitle:newsTitle,newsChnlName:newsChnl},
                    dataType:"json",
                    async : false,
                    success : function (data){
                        if(data == '0'){
                            $("#editNewsErrMSG").html("此栏目下本标题已被使用");
                        }else{
                            $("#editNewsErrMSG").html("");
                            $('#edit_news').fadeOut(100);
                        }
                    }
                })
            }
            if(newsChnl == "==请选择=="){

                $err.html("请选择栏目")
            }else if(!textTs.test($('#editNewsText').val())){

                $err.html("文本内容不得少于10个字符或多于300个字符");
            }else{
                $err.html("");
            }
            if($err.text() == "" && $("#editNewsErrMSG").text() == ""){
                window.location.href = "/doNews?method=updateNews&editNewsTitle="+$("#editNewsCenter").val()+"&editNewsKeyword="+$('#editNewsKeyword').val()+
                    "&editNewsChnlName="+$('#editNewsSelectID').val()+"&editNewsTop="+$('#editNewsSelectTop').val()+"&editNewsText="+$('#editNewsText').val()+
                    "&editNewsID="+ newsID;
            }
        }
    })

    //设置置顶
    $('label[name=newsTop]').click(function (){
         var top = $(this).text() == "[取消置顶]" ? 0:1;
         var msg = $(this).text() == "[取消置顶]" ? "确定要取消置顶吗?" : "确定要设置为置顶吗?";
         if(confirm(msg)){
             var id = $(this).parents("ul[class=list_b]").find("input[type=hidden]").attr("id");
             window.location.href = "/doNews?method=setNewsTop&setNewsTop="+top+"&setNewsID="+id;
         }
    })
//  搜索新闻
    $("#selNews_btn").click(function (){
        var newsKeyword = $('#selNews').val();
        var newsChnl = $('#selNewsChnlName').val();

        window.location.href = "/doNews?method=selNewsByKeyword&selNewsChnl="+newsChnl+"&selNewsKeyword="+newsKeyword;
    })
//删除新闻
    $('#delete_news_ok_btn').click(function (){
        var $checked = $('input[name=news]:checked');
        var delID="";
        if($checked.is(':checked')){
            $('#delete_news').fadeOut(100);
            $checked.each(function (i){
                delID = i == 0 ? delID+=$(this).next().attr('id') : delID+= ','+$(this).next().attr('id')
            })
            window.location.href = "/doNews?method=delNews&delNewsID="+delID;
        }
    })

    $('#add_news_btn').click(function (){
        $('#release_news').fadeIn(100);
    })
    $('#delete_news_btn').click(function (){
        var $checked = $('input[name=news]:checked');
        if($checked.is(':checked')){
            $('#delete_news').fadeIn(100);
        }
    })
    $('#edit_news_btn').click(function (){
        var $checked = $('input[name=news]:checked');
        if($checked.is(':checked')){
            var check = $checked.eq(0);
            var find = check.parents("ul[class=list_b]").find('label[name="chnlName"]');
            var top = check.nextAll(":last").prev().text() == '[已置顶]' ? "是":"否"
            $("#editNewsCenter").val(check.nextAll(":last").text());
            $('#editNewsKeyword').val(check.next().next().attr("name"));
            $('#editNewsSelectID').val(find.text());
            $('#editNewsSelectTop').val(top);
            $('#editNewsText').val(find.next().val());
            $('#edit_news').fadeIn(100);
        }
    })
})
